package application.client.gui.components;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.rmi.server.ServerNotActiveException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.Border;

import middleware.datacontainer.searchclient.ClientContainer;

import application.client.gui.interfaces.IChoosable;
import application.controller.client.GuiController;
import application.exceptions.DatabaseException;
import application.exceptions.UserNotInDbException;
import application.exceptions.WrongCredentialsException;

public class LoginComponents extends JPanel implements IChoosable {

	private JButton loginButton;
	private JButton settingButton;
	private JButton messageButton;
	private JLabel loginInfo;
	
	public LoginComponents()
	{
		setLayout(new BorderLayout());
		setBackground(Color.white);
		loginInfo=new JLabel("",SwingConstants.RIGHT);
		loginInfo.setVerticalAlignment(SwingConstants.BOTTOM);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setPreferredSize(new Dimension(100,90));
		buttonPanel.setBackground(Color.white);	
		
		
		loginButton=new JButton();
		loginButton.setPreferredSize(new Dimension(100,27));
		loginButton.setText("Login");
		
		settingButton=new JButton();
		settingButton.setPreferredSize(new Dimension(100,27));
		settingButton.setText("Settings");
		settingButton.setEnabled(false);
		
		messageButton=new JButton();
		messageButton.setPreferredSize(new Dimension(100,27));
		messageButton.setText("Messages");
		messageButton.setEnabled(false);
		
		buttonPanel.add(settingButton);
		buttonPanel.add(messageButton);
		buttonPanel.add(loginButton);
		
		JPanel dummyPanel=new JPanel();
		dummyPanel.setBackground(Color.white);
		dummyPanel.setPreferredSize(new Dimension(200,1));
		add(dummyPanel,BorderLayout.NORTH);
		
		add(loginInfo,BorderLayout.CENTER);
		add(buttonPanel,BorderLayout.EAST);		
				
		loginButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				if(GuiController.getInstance().getCurrentSessionID()==null){
					getLoginData();					
				}
				else
				{
					logout();
				}
			}
		
		});
		settingButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				GuiController.getInstance().addSettingTab();				
			}
			
		});
		messageButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				GuiController.getInstance().addMessageTab();				
			}
			
		});
	}
	private void getLoginData()
	{
		new LoginFrame(this);
	}

	@Override
	public void reserveMedium(ClientContainer CC) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void loan(ClientContainer CC) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void login(String user, String pass) {
			GuiController.getInstance().login(user, pass);
			
			loginInfo.setText("logged in as "+user+".");
			loginButton.setText("Logout");
			settingButton.setEnabled(true);
			messageButton.setEnabled(true);
		
	}
	private void logout()
	{
		loginInfo.setText("");
		loginButton.setText("Login");
		GuiController.getInstance().resetSessionID();
		settingButton.setEnabled(false);
		messageButton.setEnabled(false);
	}
}
